package com.hs.jhzd.controller;

import com.hs.jhzd.entity.Disease;
import com.hs.jhzd.entity.DiseaseCategory;
import com.hs.jhzd.entity.Meeting;
import com.hs.jhzd.repository.DiseaseCategoryRepository;
import com.hs.jhzd.repository.DiseaseRepository;
import com.hs.jhzd.repository.MeetingNativeRepository;
import com.hs.jhzd.repository.ZfYyjlbRepository;
import com.hs.jhzd.utils.WebResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@RequestMapping("/m")
@Controller
public class MeetingController {

//  @Autowired
//  private MeetingRepository repository;

  @Autowired
  private MeetingNativeRepository nativeRepository;

  @Autowired
  private DiseaseCategoryRepository diseaseCategoryRepository;

  @Autowired
  private DiseaseRepository diseaseRepository;
  @Autowired
  private ZfYyjlbRepository zfYyjlbRepository;
  public static final String DATE_FORMAT = "yyyyMMdd";
  public static final String DATE_FORMAT1 = "yyyy-MM-dd HH:mm:ss";

//  @RequestMapping("/findByJsbh")
//  @ResponseBody
//  public List<Meeting> findByJsbh(String jsbh) {
//    return repository.findByJsbh(jsbh);
//  }
//
//  @RequestMapping("/findByRybh")
//  @ResponseBody
//  public List<Meeting> findByRybh(String jsbh, String rybh) {
//    return repository.findByJsbhAndRybh(jsbh, rybh);
//  }
//
//  @RequestMapping("/save")
//  @ResponseBody
//  public boolean save(Meeting meeting) {
//    meeting.setDate(new Date());
//
//    repository.save(meeting);
//    return true;
//  }

  /**
   * 
   * @param jsbh
   * @param rybh
   * @param name
   * @param startTime
   * @param endTime
   * @param type 检察官、所领导、管教、律师、医生
   * @param done 是否已处理
   * @return
   */
  @RequestMapping("/search")
  @ResponseBody
  public List<Meeting> search(String jsbh, String rybh, String name,
                                    @DateTimeFormat(pattern = DATE_FORMAT1) Date startTime,
                                    @DateTimeFormat(pattern = DATE_FORMAT1) Date endTime, String type, Boolean isDisease,
                                    Boolean done) {
    return nativeRepository.search(jsbh, rybh, name, startTime, endTime, type, isDisease, done);
  }

  @RequestMapping("/admin/main")
  public String shiftMainPage(Model model, String jsbh, String rybh, String name,
      @DateTimeFormat(pattern = DATE_FORMAT) Date startTime,
      @DateTimeFormat(pattern = DATE_FORMAT) Date endTime, String type, Boolean isDisease,
      Boolean done) {
    List<Meeting> meetings = search(jsbh, rybh, name, startTime, endTime, type, isDisease, done);
    model.addAttribute("jsbh", jsbh);
    model.addAttribute("results", meetings);
    return "meeting-list";
  }

  @RequestMapping("/diseaseCategory")
  @ResponseBody
  public List<DiseaseCategory> findDiseaseCategory() {
    return diseaseCategoryRepository.findAll();
  }

  @RequestMapping("/diseases")
  @ResponseBody
  public List<Disease> findDiseases(String categoryCode) {
    if (StringUtils.isBlank(categoryCode)) {
      return null;
    }
    return diseaseRepository.findByCategoryCode(categoryCode);
  }
  /*
   * 预约的处理操作
   */
  @RequestMapping("/dispose")
  @ResponseBody
  public WebResult disposeAppointment(String rybh,@DateTimeFormat(pattern = DATE_FORMAT1) Date startTime, Integer status) {
    Date endTime = new Date();
    int num = zfYyjlbRepository.updateDispose(rybh,startTime,status,endTime);
	if(num==0) {
		return WebResult.FAIL("修改失败");
	}else {
		return WebResult.OK();
	}
  }

}
